import Vue from "vue";

// 生成随机数 min最小值，max最大值
const randomRange = (min = 0, max = 10) => {
  return Math.floor(Math.random() * (max - min)) + min;
};

// 生成 uuid
const uuid = () => {
  var s = [];
  var hexDigits = "0123456789abcdef";
  for (var i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = "4";
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
  s[8] = s[13] = s[18] = s[23] = "-";

  var uuid = s.join("");
  return uuid;
};

/**
 *  设置样式对象
 * @params 需要解析的对象
 * @params 需要解析的属性，不传则解析所有
 * @returns 返回样式对象
 */
const setStyle = (
  data,
  typeList = [
    "color",
    "height",
    "width",
    "borderColor",
    "borderStyle",
    "borderWidth",
    "fontWeight",
    "fontSize",
    "left",
    "top",
    "bottom",
    "right",
    "letterSpacing",
    "lineHeight",
    "backgroundColor",
    "borderRadius",
    "zIndex",
    "textShadow",
    "boxShadow",
    "fontFamily",
    "opacity",
    "display",
    "alignItems",
    "justifyContent",
    "textAlign",
    "rotate",
    "scale",
  ]
) => {
  const styleObj = {};
  function _hasStyle(attr) {
    if (data.hasOwnProperty(attr)) {
      if (attr === "zIndex") {
        styleObj[attr] = data[attr];
      } else if (attr === "opacity") {
        styleObj[attr] = data[attr] / 100;
      } else if (attr == "scale") {
        styleObj["transform"] = data[attr];
      }
      // else if (attr === "rotate") {
      //   styleObj["transform"] = `rotate(${data[attr]}deg)`;
      // }
      else {
        if (typeOf(data[attr]) === "String") {
          styleObj[attr] = `${data[attr]}`;
        } else if (typeOf(data[attr]) === "Number") {
          styleObj[attr] = `${data[attr]}px`;
        }
      }
    }
  }
  for (let item in data) {
    if (typeList.includes(item)) {
      _hasStyle(item);
    }
  }

  return styleObj;
};

/**
 *  判断类型
 * @params 需要判断类型的对象
 * @returns  返回类型
 */
 const typeOf = data => {
  let type = "";
  if (Object.prototype.toString.call(data) === "[object Number]") {
    type = "Number";
  } else if (Object.prototype.toString.call(data) === "[object String]") {
    type = "String";
  } else if (Object.prototype.toString.call(data) === "[object Undefined]") {
    type = "Undefined";
  } else if (Object.prototype.toString.call(data) === "[object Array]") {
    type = "Array";
  } else if (Object.prototype.toString.call(data) === "[object Null]") {
    type = "Null";
  } else if (Object.prototype.toString.call(data) === "[object Object]") {
    type = "Object";
  } else {
    // console.log(data);
    // console.log(Object.prototype.toString.call(data));
  }
  return type;
};

/**
 *  处理时间戳转成相应得时间格式
 *  此处仅针对yyyy-MM-dd hh:mm:ss 的格式进行格式化
 * @returns 返回想要得时间格式
 */
const dateFormat = function (time, format) {
  var date = new Date(time);
  var year = date.getFullYear();
  // 在日期格式中，月份是从0开始的，因此要加0, 使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
  var month =
    date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1;
  var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  var minutes =
    date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  var seconds =
    date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

  if (1 === format) {
    return (
      year +
      "-" +
      month +
      "-" +
      day +
      " " +
      hours +
      ":" +
      minutes +
      ":" +
      seconds
    );
  } else if (2 === format) {
    return year + "-" + month + "-" + day;
  } else if (3 === format) {
    return hours + ":" + minutes + ":" + seconds;
  } else if (4 === format) {
    return weekFormat(time);
  } else if (5 === format) {
    return hours + ":" + minutes;
  } else if (6 === format) {
    return year;
  } else if (7 === format) {
    return month;
  }
};

// start 时间转换星期
const weekFormat = function (time) {
  let myDate = new Date(time);
  let wk = myDate.getDay();
  let weeks = [
    "星期日",
    "星期一",
    "星期二",
    "星期三",
    "星期四",
    "星期五",
    "星期六",
  ];
  return weeks[wk];
};

Vue.prototype.$methods = {
  randomRange,
  uuid,
  setStyle,
  dateFormat,
  typeOf
};
